/**
 *====================================================
* 文件名称: AbnormalRiskServiceImpl.java
 * 修订记录：
  * No    日期				作者(操作:具体内容)
  * 1.    2015年12月14日	chenxy(创建:创建文件)
  *====================================================
  * 类描述：(说明未实现或其它不应生成javadoc的内容)
  */
package com.yuexunit.finance.project.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.project.entity.AbnormalRiskEntity;
import com.yuexunit.finance.project.entity.lexicon.AuthenticatStateEnum;
import com.yuexunit.finance.project.entity.lexicon.SettlementStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: AbnormalRiskServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年12月14日 14:46:00
 */
@Implements(AbnormalRiskService.class)
public class AbnormalRiskServiceImpl implements AbnormalRiskService {

	@Override
	public Long createAbnormalRisk(AbnormalRiskEntity entity) {
		if (AuthenticatStateEnum.CONFIRMED != entity.getAuthenticatState()) {
			entity.setAuthenticatState(AuthenticatStateEnum.UNCONFIRM);
		}
		if (entity.getCreateDateTime() == null) {
			entity.setCreateDateTime(DateUtil.now());
		}
		PersistUtil.insert(entity);
		return entity.getAbnormalRiskId();
	}

	@Override
	public void updateAbnormalRisk(AbnormalRiskEntity entity) {
		AbnormalRiskEntity persist = inquireAbnormalRisk(entity.getAbnormalRiskId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	public void renovateAbnormalRisk(AbnormalRiskEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public AbnormalRiskEntity inquireAbnormalRisk(Long abnormalRiskId) {
		return PersistUtil.get(AbnormalRiskEntity.class, abnormalRiskId);
	}

	@Override
	public List<AbnormalRiskEntity> inquireAbnormalRiskList(Long abnormalId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(AbnormalRiskEntity.class);
		criteria.add(Restrictions.eq("abnormalId", abnormalId));
		return PersistUtil.get(criteria);
	}

	@Override
	public PageList<AbnormalRiskEntity> inquireAbnormalRiskPageList(Long abnormalId, Integer publish, SettlementStateEnum settlementState, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(AbnormalRiskEntity.class);
		if (abnormalId != null && abnormalId != 0) {
			criteria.add(Restrictions.eq("abnormalId", abnormalId));
		}
		if (publish != null) {
			criteria.add(Restrictions.eq("publish", publish));
		}
		if (settlementState != null) {
			criteria.add(Restrictions.eq("settlementState", settlementState));
		}
		return PersistUtil.get(criteria, page);
	}

	@Override
	public PageList<AbnormalRiskEntity> inquireAbnormalRiskSettledPageList(Long abnormalId, Integer publish, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(AbnormalRiskEntity.class);
		if (abnormalId != null && abnormalId != 0) {
			criteria.add(Restrictions.eq("abnormalId", abnormalId));
		}
		if (publish != null) {
			criteria.add(Restrictions.eq("publish", publish));
		}
		criteria.add(Restrictions.gt("settlementState", SettlementStateEnum.UNKNOWN));
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<Long> countRsiskWarnings() {
		DetachedCriteria criteria = DetachedCriteria.forClass(AbnormalRiskEntity.class);
		criteria.add(Restrictions.eq("settlementState", SettlementStateEnum.UNKNOWN));
		criteria.add(Restrictions.eq("publish", 1));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("abnormalRiskId")));
		return PersistUtil.get(criteria);
	}
}
